package pers.corvey.crawler.transformer;

import java.util.LinkedList;
import java.util.List;

import pers.corvey.crawler.model.Movie;
import pers.corvey.crawler.model.json.MovieJson;
import pers.corvey.crawler.util.RegularExpressionUtils;

public class MovieTransformer {
	
	public static Movie toMovie(MovieJson json) {
		if (json == null) {
			return null;
		}
		Movie movie = new Movie();
		movie.setId(json.getId());
		movie.setTitle(json.getTitle());
		if (json.getRating() != null) {
			movie.setAverage(json.getRating().getAverage());
			movie.setStars(json.getRating().getStars());
		}
		movie.setRatingsCount(json.getRatings_count());
		if (json.getImages() != null) {
			movie.setImage(json.getImages().getLarge());
		}
		movie.setSubtype(json.getSubtype());
		movie.setDirectors(CelebrityTransformer.toCelebrity(json.getDirectors()));
		movie.setCasts(CelebrityTransformer.toCelebrity(json.getCasts()));
		movie.setYear(RegularExpressionUtils.extractYear(json.getYear()));
		movie.setGenres(GenreTransformer.toGenre(json.getGenres()));
		movie.setSummary(json.getSummary());
		return movie;
	}
	
	public static List<Movie> toMovie(List<MovieJson> jsons) {
		if (jsons == null || jsons.size() == 0) {
			return null;
		}
		List<Movie> movies = new LinkedList<>();
		for (MovieJson json : jsons) {
			if (json.getId() != null) {
				movies.add(toMovie(json));
			}
		}
		return movies;
	}
}
